<?php
class ProductController extends Core\Api{

    //图书分类
    public function categoryAction(){
        //图书分类
        $style = $this->post('style','tree');
        if ($style == 'tree') {
        $category = \Business\CategoryModel::getTree(1);
        } else {
            $category = \Business\CategoryModel::getList(1);
        }
        $data =$category;
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    //最近分类
    public function latest_categoryAction() {
        $category_latest = \Business\CategoryModel::getLatest(10);
        $ret = $this->ok($category_latest);
        return $this->json($ret);
    }
    //最近列表
    public function latestAction() {
        $cid = $this->post('category_id');
        $page_size = $this->post('per_page');
        Paginator::currentPageResolver(function() {
            return $this->post('page');
        });
        //最新上架$latest;
        $data = $latest = \Business\ProductModel::getLatestList($cid,$page_size);
        $ret = $this->ok($data['data']);
        $ret['page'] = $data['page'];
        return $this->json($ret);
    }
    //最近列表
    public function featuredAction() {
        $page_size = $this->post('per_page');
        Paginator::currentPageResolver(function() {
            return $this->post('page');
        });
        //经典热销
        $featured = \Business\ProductModel::getFeaturedList($page_size);
        $data = $featured;
        $ret = $this->ok($data['data']);
        $ret['page'] = $data['page'];
        return $this->json($ret);
    }
    //特价专区分类
    public function discount_categoryAction() {
        $discount_category = array(
            array(
                'name'  =>'免费',
                'min'   => 0.00,
                'max'   => 0.00,
            ),
            array(
                'name'  =>'0-2元区',
                'min'   => 0.01,
                'max'   => 2.99,
            ),
            array(
                'name'  =>'3-5元区',
                'min'   => 3.00,
                'max'   => 5.00,
            ),
            array(
                'name'  =>'5元以上',
                'min'   => 5.01,
                'max'   => null,
            ),
        );
        $data = $discount_category;
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    public function recommendAction() {
        $page_size = $this->post('per_page');
        Paginator::currentPageResolver(function() {
            return $this->post('page');
        });
        //最新上架$latest;
        $data = $latest = \Business\ProductModel::getRecommendList($page_size);
        $ret = $this->ok($data['data']);
        $ret['page'] = $data['page'];
        return $this->json($ret);
    }

    //特价专区
    public function discountAction() {
        $max = $this->post('max');
        $min = $this->post('min');
        $page_size = $this->post('per_page');
        Paginator::currentPageResolver(function() {
            return $this->post('page');
        });
        $data = $discount = \Business\ProductModel::getDiscountList($min,$max,$page_size);
        $ret = $this->ok($data['data']);
        $ret['page'] = $data['page'];
        return $this->json($ret);
    }
    //详情
    public function itemAction(){
        $product_id = $this->post('product_id');
        $return_category = $this->post('return_category',0);
        $data = \Business\ProductModel::getProduct($product_id,$return_category);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    //详情分类
    public function itemCategoryParents(){
        $product_id = $this->post('product_id');
        $data = \Business\ProductModel::getProduct($product_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    //添加到购物车
    public function addToCartAction(){
        $user_id = $this->session->user['id'];
        $product_id = $this->post('product_id');
        if(!$product_id){
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $product_discount_id = $this->post('product_discount_id',0);
        $quantity   = 1;
        if($user_id) {
            //如果是登录用户
            $res = CartModel::setCart($user_id,$product_id,$quantity,$product_discount_id);
            if (!$res) {
                $ret = $this->error('');
                return $this->json($ret);
            }
            $ret = $this->ok();
            return $this->json($ret);
        } else {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
    }
    //购物车
    public function cartAction(){
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $data = Business\UserModel::getCart($user_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    //购物车删除
    public function delFromCartAction(){
        $user_id = $this->session->user['id'];
        $product_id = $this->post('product_id');
        if(!$product_id){
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        if($user_id) {
            //如果是登录用户
            $res = \Business\UserModel::delFromCart($user_id,$product_id);
            if (!$res) {
                $ret = $this->error('删除失败');
                return $this->json($ret);
            }
            $ret = $this->ok();
            return $this->json($ret);
        } else {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
    }
    //收藏
    public function addToFavouriteAction() {
        $user_id = $this->session->user['id'];
        $product_id = $this->post('product_id');
        if(!$product_id){
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $product_discount_id = $this->post('product_discount_id',0);
        $quantity   = 1;

        if($user_id) {
            //如果是登录用户
            $res = \Business\UserModel::addFavourite($user_id,$product_id);
            if (!$res) {
                $ret = $this->error('更新失败');
                return $this->json($ret);
            }
            $ret = $this->ok();
            return $this->json($ret);
        } else {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
    }
    //收藏
    public function delFromFavouriteAction() {
        $user_id = $this->session->user['id'];
        $product_id = $this->post('product_id');
        if(!$product_id){
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        if($user_id) {
            //如果是登录用户
            $res = \Business\UserModel::delFromFavourite($user_id,$product_id);
            if (!$res) {
                $ret = $this->error('删除失败');
                return $this->json($ret);
            }
            $ret = $this->ok();
            return $this->json($ret);
        } else {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
    }
    //收藏
    public function favouriteAction(){
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $data = Business\UserModel::getFavourite($user_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    //评论
    public function commentAction(){
        $product_id = $this->post('product_id');
        $per_page = $this->post('per_page');
        $rank = strtoupper($this->post('rank','ALL'));
        Paginator::currentPageResolver(function() {
            return $this->post('page');
        });
        if(!$product_id){
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $data = Business\ProductModel::getComment($product_id,$rank,$per_page);
        $ret = $this->ok($data['data']);
        $ret['page'] = $data['page'];
        return $this->json($ret);
    }
    //评论统计
    public function commentCountAction() {
        $product_id = $this->post('product_id');
        $data = Business\ProductModel::getCommentCount($product_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    //添加评论
    public function addToCommentAction(){
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $product_id = $this->post('product_id');
        $title = $this->post('title','');
        $content = $this->post('content','');
        $score = $this->post('score');
        if(!$product_id){
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $res = Business\ProductModel::addToComment($user_id,$product_id,$title,$content,$score);
        if (!$res) {
            $ret = $this->error('已经评价过此商品，或此商品已经下架');
            return $this->json($ret);
        }
        $ret = $this->ok();
        return $this->json($ret);
    }
    public function searchAction(){
        $keyword = $this->post('keyword');
        $orderby = $this->post('orderby');
        $page_size = $this->post('per_page',10);
        $data = \Business\ProductModel::getSearch($keyword,$orderby,$page_size);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    public function categoryParentsAction(){
        $category = \Business\CategoryModel::getParents(4);
        $data =$category;
        $ret = $this->ok($data);
        return $this->json($ret);
    }

}
